package com.demo.impl.service;

import com.demo.api.user.domain.TestLimit;
import com.demo.api.user.domain.TestMultiLimitDto;
import com.demo.api.user.domain.TestUser;
import com.demo.api.user.service.TestMultiLimitService;
import com.demo.api.user.service.UserService;
import com.demo.common.enums.ResultCode;
import com.demo.common.response.Result;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;

/**
 *
 * @author
 * @since
 */
@Component
@Slf4j
public class OpenNewUserMDMTServiceImpl implements OpenNewUserMDMTService {

  @Reference(version = "1.0.0", check = false)
  private UserService userService;

  @Reference(version = "1.0.0", check = false)
  private TestMultiLimitService testMultiLimitService;

  @Override
  public void addLimitCommit() {

  }

  @SneakyThrows
  @Override
  @GlobalTransactional(rollbackFor = Exception.class)
  @ShardingTransactionType(TransactionType.BASE)
  public void addUserAndLimitRollback(String idno) {
    log.info("OpenNewUserMDMTServiceImpl seata 回滚测试开始");
    log.info("分布式事务, xid:{}", RootContext.getXID());
    log.info("新增用户信息");
    // add user
    TestUser testUser = new TestUser();
    testUser.setFullName("");
    testUser.setIdno(idno);
    testUser.setSex("3");
    Result result = userService.addUser(testUser);
    log.info("{}", result);
    if (!ResultCode.SUCCESS.code().equals(result.getStatus())) {
      throw new Exception("创建用户失败");
    }

    log.info("新增用户信息成功，现在新增额度信息");
    List<TestMultiLimitDto> testMultiLimitList = new ArrayList<>(64);
    for (int i = 1; i < 5; i++) {
      testMultiLimitList.add(TestMultiLimitDto.builder()
          .libId(1)
          .tabId(i)
          .idno(idno)
          .dt(new Date())
          .limitAmount(new BigDecimal((new Random()).nextFloat()))
          .build());
    }

    result = testMultiLimitService.addTestLimit(testMultiLimitList);

    throw new Exception("触发异常回滚");

  }

  @SneakyThrows
  @Override
  @GlobalTransactional
  @ShardingTransactionType(TransactionType.BASE)
  public void addUserAndLimit(String idno) {
    log.info("分布式事务, xid:{}", RootContext.getXID());
    log.info("新增用户信息");
    // add user
    TestUser testUser = new TestUser();
    testUser.setFullName("");
    testUser.setIdno(idno);
    testUser.setSex("3");
    Result result = userService.addUser(testUser);
    log.info("{}", result);
    if (!ResultCode.SUCCESS.code().equals(result.getStatus())) {
      throw new Exception("创建用户失败");
    }

    log.info("新增用户信息成功，现在新增额度信息");
    List<TestMultiLimitDto> testMultiLimitList = new ArrayList<>(64);
    for (int i = 1; i < 5; i++) {
      testMultiLimitList.add(TestMultiLimitDto.builder()
          .libId(1)
          .tabId(i)
          .idno(idno)
          .dt(new Date())
          .limitAmount(new BigDecimal((new Random()).nextFloat()))
          .build());
    }

    result = testMultiLimitService.addTestLimit(testMultiLimitList);
    log.info("{}", result);
    if (!ResultCode.SUCCESS.code().equals(result.getStatus())) {
      throw new Exception("创建额度失败");
    }
    log.info("新增额度信息成功，流程结束");
  }
}
